package com.citicpub_cloud.restfull.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.citicpub_cloud.restfull.dao.*;
import com.citicpub_cloud.restfull.dao.core.ContentTrack;
import com.citicpub_cloud.restfull.dao.core.EbookFile;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.citicpub_cloud.restfull.api.dto.ChapterDTO;
import com.citicpub_cloud.restfull.api.dto.MixResDTOExt;
import com.citicpub_cloud.restfull.service.ResourceService;

@Service
public class ResourceServiceImpl implements ResourceService {

	@Autowired
	ContentTrackDaoService contentTrackDaoService;

	@Override
	public MixResDTOExt getResourceContentById(int gtype, Long contentAlbumId, String gname, String gimg) {
		MixResDTOExt obj = new MixResDTOExt();
		obj.setTitle(gname);
		obj.setImgUrl(gimg);

		List<ChapterDTO> clist = getChapterList(contentAlbumId,gtype,gname);

		obj.setChapterlist(clist);
//		if(1 == gtype){//有声书
//			obj = bookAudioDaoService.getBaseInfo(id);
//
//			if(null != obj){
//				obj.setTitle(gname);
//				obj.setImgUrl(gimg);
//
//				List<ChapterDTO> clist = bookAudioDaoService.getChapterList(id);
//				obj.setChapterlist(clist);
//			}
//		}
//
//		if(2 == gtype){//视频读书
//			TbVideoInfo tmpob = videoDaoService.getBaseInfo(id);
//
//			if(null != tmpob) {
//				obj = new MixResDTOExt();
//
//				obj.setTitle(gname);
//				obj.setImgUrl(gimg);
//
//				ChapterDTO chapterobj = new ChapterDTO();
//				chapterobj.setTitle(gname);
//				chapterobj.setOrdernum("");
//				chapterobj.setChapterUrl(tmpob.getVideourl());
//				chapterobj.setChapterSize(tmpob.getVideoSize());
//
//				List<ChapterDTO> clist = new ArrayList<ChapterDTO>();
//				clist.add(chapterobj);
//
//				obj.setChapterlist(clist);
//			}
//		}
//		if(6 == gtype){//电子书
//			TbBookInfo tmpob = ebookDaoService.getBaseInfo(id);
//
//			if(null != tmpob) {
//				obj = new MixResDTOExt();
//
//				obj.setTitle(gname);
//				obj.setImgUrl(gimg);
//
//				ChapterDTO chapterobj = new ChapterDTO();
//				chapterobj.setTitle(gname);
//				chapterobj.setOrdernum("");
//				chapterobj.setChapterUrl(tmpob.getFileUrl());
//				chapterobj.setChapterSize(tmpob.getFileSize());
//
//				List<ChapterDTO> clist = new ArrayList<ChapterDTO>();
//				clist.add(chapterobj);
//
//				obj.setChapterlist(clist);
//			}
//		}
//		if(9 == gtype){//好书快听
//			TbAudioResInfo tmpob = audioResDaoService.getBaseInfo(id);
//
//			if(null != tmpob) {
//				obj = new MixResDTOExt();
//
//				obj.setTitle(gname);
//				obj.setImgUrl(gimg);
//
//				ChapterDTO chapterobj = new ChapterDTO();
//				chapterobj.setTitle(gname);
//				chapterobj.setOrdernum("");
//				chapterobj.setChapterUrl(tmpob.getAudioUrl());
//				chapterobj.setChapterSize(tmpob.getAudioSize());
//
//				List<ChapterDTO> clist = new ArrayList<ChapterDTO>();
//				clist.add(chapterobj);
//
//				obj.setChapterlist(clist);
//			}
//		}
//		if(10 == gtype){//阅读时差
//			TbAudioResInfo tmpob = audioResDaoService.getBaseInfo(id);
//
//			if(null != tmpob) {
//				obj = new MixResDTOExt();
//
//				obj.setTitle(gname);
//				obj.setImgUrl(gimg);
//
//				ChapterDTO chapterobj = new ChapterDTO();
//				chapterobj.setTitle(gname);
//				chapterobj.setOrdernum("");
//				chapterobj.setChapterUrl(tmpob.getAudioUrl());
//				chapterobj.setChapterSize(tmpob.getAudioSize());
//
//				List<ChapterDTO> clist = new ArrayList<ChapterDTO>();
//				clist.add(chapterobj);
//
//				obj.setChapterlist(clist);
//			}
//		}
		
		return obj;
	}

	private List<ChapterDTO> getChapterList(Long contentAlbumId,int gtype,String gname) {

		if(gtype == 6){
			EbookFile ebookFile = 	contentTrackDaoService.getEbook(contentAlbumId);
			if(ebookFile == null){
				return new ArrayList<>();
			}else{
				List<ChapterDTO> chapterDTOS = new ArrayList<>(1);
				ChapterDTO chapterDTO = new ChapterDTO();
				chapterDTO.setTitle(gname);
				chapterDTO.setChapterSize(ebookFile.getFileSize()== null ? 0:ebookFile.getFileSize());
				chapterDTO.setChapterUrl(ebookFile.getFileUrl());
				chapterDTO.setOrdernum("");
				chapterDTOS.add(chapterDTO);
				return chapterDTOS;
			}
		}else {
			List<ContentTrack> contentTrackList = contentTrackDaoService.getContentTrackByAlbumId(contentAlbumId);

			List<ChapterDTO> chapterDTOS = new ArrayList<>(contentTrackList.size());
			for (ContentTrack contentTrack : contentTrackList) {

				if(StringUtils.isNotBlank(contentTrack.getTrackFileUrl())) {
					ChapterDTO chapterDTO = new ChapterDTO();
					chapterDTO.setTitle(contentTrack.getTrackTitle());
					chapterDTO.setChapterSize(contentTrack.getFileSize() == null ? 0:contentTrack.getFileSize());
					chapterDTO.setChapterUrl(contentTrack.getTrackFileUrl());
					chapterDTO.setOrdernum(contentTrack.getOrderNum().toString());
					chapterDTOS.add(chapterDTO);
				}
			}
			return chapterDTOS;
		}
	}

}
